import {
  Enumerator
} from './enumeration'
import ExcelPort from './excelPort'

/**
 * @description: Date  Format
 */
const DateTimeFormat = (time, fmt = 'yyyy-MM-dd', type) => {
  // time 时间戳 date string
  // fmt 'yyyy-MM-dd hh:mm:ss'
  // type 1 return string 2 return obj
  if (!time) return ''
  if (typeof time === 'number' && time.toString().length < 12) {
    // 小于13位的时间戳
    time *= 1000
  }
  // 2020-11-05T03:47:55.000+0000 世界时间
  const userAgent = navigator.userAgent
  const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  const isSafari = userAgent.indexOf('Safari') > -1 // Safari终端
  if (typeof time === 'string') {
    if (time.indexOf('T') > -1 && time.indexOf('.000') > -1 && time.indexOf('+0000') > -1) {
      if (isiOS || isSafari) {
        time = time.replace(/-/g, '/').replace(/T/g, ' ').replace(/\.\d{3}\+/, '+')
      }
    }
  }

  let date = new Date(time)
  let o = {
    'y': date.getFullYear(),
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  // type 存在 导出时间对象
  if (type) {
    for (let i in o) {
      if (o[i] < 10) {
        o[i] = '0' + o[i].toString()
      }
    }
    return o
  }

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

/**
 * @description: array unique
 */
export function arrayUnique (array, type) {
  if (type === 'indexof') {
    var temp = []
    for (var i = 0; i < array.length; i++) {
      if (temp.indexOf(array[i]) === -1) {
        temp.push(array[i])
      }
    }
    return temp
  } else {
    for (let i = 0; i < array.length; i++) {
      for (let j = i + 1; j < array.length; j++) {
        if (array[i].key === array[j].key) {
          array.splice(j, 1)
          j--
        }
      }
    }
    return array
  }
}

/**
 * @description: Element Table Sum
 */
const TableGetSummaries = (param, config) => {
  config = config || {
    totalWord: '总计',
    isNaNWord: 'N/A',
    nextWrod: ''
  }
  const {
    columns,
    data
  } = param
  const sums = []
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = config.totalWord || '总计'
      return
    }
    const values = data.map(item => Number(item[column.property]))
    if (!values.every(value => isNaN(value))) {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!isNaN(value)) {
          return prev + curr
        } else {
          return prev
        }
      }, 0)
      sums[index] += ' ' + config.nextWrod || ''
    } else {
      sums[index] = config.isNaNWord || 'N/A'
    }
  })
  return sums
}

/**
 * @description: return array
 */
const ExportList = (key) => {
  let arr = Enumerator[key] || []
  return arr
}

/**
 * @description: return array => label
 */
const ExportStr = (key, objKey, errStr) => {
  errStr = errStr || '--'
  key = key.toString()

  let array = []
  // 检查是否有对应的text map，没有的话直接返回
  if (Enumerator.hasOwnProperty(objKey)) {
    array = Enumerator[objKey]
  } else {
    // array = []
    return errStr
  }
  let str = ''
  array.map(element => {
    if (element.value === key) {
      str = element.label
    }
  })
  str = str || errStr
  return str
}

const NowDateTime = () => {
  let DateTimeStr = ''
  const TodayStr = DateTimeFormat(new Date(), 'yyyy-MM-dd')
  var d = new Date(TodayStr.replace(/-/g, '/'))
  var todaysDate = new Date()
  if (d.setHours(0, 0, 0, 0) === todaysDate.setHours(0, 0, 0, 0)) {
    // 是今天
    DateTimeStr = DateTimeFormat(new Date(), 'hh:mm:ss')
  } else {
    DateTimeStr = '23:59:59'
  }
  return DateTimeStr
}

export {
  DateTimeFormat,
  NowDateTime,
  TableGetSummaries,
  ExportList,
  ExportStr,
  ExcelPort
}
